package com.example.seckill.Util;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

@Component
public class MD5Util {
    public static String md5(String str) {
        return DigestUtils.md5Hex(str);
    }

    private static final String salt = "1a2b3c4d";

    /**
     * 第一次加密,前端密码转成后端密码
     * @param inputPass
     * @return java.lang.String
     * @operation add
     **/
    public static String inputPassToFromPass(String inputPass) {
        //随意加密
       // System.out.println(salt);

        String str = ""+salt.charAt(0) + salt.charAt(4) + inputPass + salt.charAt(1) + salt.charAt(5);
//        System.out.println("str="+str);
        return md5(str);
    }

    /**
     * 第二次加密，后端到数据库密码
     * @param formPass
     * @param salt
     * @return java.lang.String
     **/
    //这里的盐是数据库里的盐
    public static String formPassToDBPass(String formPass, String salt) {
//        System.out.println(salt);
        String str = salt.charAt(0) + salt.charAt(4) + formPass + salt.charAt(1) + salt.charAt(5);
//        System.out.println("str="+str);
        return md5(str);
    }

    public static String inputPassToDBPass(String inputPass, String salt) {
        String fromPass = inputPassToFromPass(inputPass);
        String dbPass = formPassToDBPass(fromPass, salt);
        return dbPass;
    }
////test
    public static void main(String[] args){
        String ps="12345";
        String salt="cX$rTe6y";
//        System.out.println(inputPassToFromPass(ps));
        String formPass=inputPassToFromPass(ps);
        System.out.println(formPassToDBPass(formPass,salt));
        System.out.println(inputPassToDBPass(ps,salt));
//        System.out.println("不加密码："+ps);

//        System.out.println(inputPassToDBPass(ps,salt));
//        System.out.println("db="+inputPassToDBPass(ps,));
//        System.out.println("后端="+inputPassToFromPass(ps));


    };
}
    //test
//    public static void main(String[] args){
//        System.out.println();
//        System.out.println(inputPassToFromPass("12345")
//        System.out.println(md5("12345")
//        );
//        System.out.println(formPassToDBPass("f89359f7f5189f0806385f87f3c4663e","1a2b3c4d"));
//        System.out.println(inputPassToDBPass("12345","1a2b3c4d"));
//    };
